package com.justgame.project.cloud.common.util;

import com.alibaba.fastjson.JSONObject;
import com.justgame.project.cloud.base.entity.MgMessageQueueLog;
import org.springframework.amqp.core.Message;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;

/**
 * @author ZhouWeiBo
 * @create 2022/5/31
 */
public class RabbitHelper {

    /**
     * 获取消息id
     * @param message
     * @return
     */
    public static String getMsgId(Message message){
        return message.getMessageProperties().getMessageId();
    }

    /**
     * 是否是已消费过的消息
     * @param message 消息
     * @return Boolean 布尔值
     */
    public static Boolean isExecutedMsg(Message message){
        MongoTemplate mongoTemplate = AppContextUtils.getBean(MongoTemplate.class);
        MgMessageQueueLog mongo = mongoTemplate.findById(getMsgId(message), MgMessageQueueLog.class);
        return mongo != null && mongo.getExecuteSuccess() != null && mongo.getExecuteSuccess();
    }

    /**
     * 更新Mongo，将此消息更新为已执行成功
     * @param message 消息
     * @return Boolean 布尔值
     */
    public static void execute(Message message){
        MongoTemplate mongoTemplate = AppContextUtils.getBean(MongoTemplate.class);
        /* 查询条件 */
        Query query = new Query();
        query.addCriteria(
                Criteria.where("_id").is(RabbitHelper.getMsgId(message))
        );
        /* 更新字段 */
        Update update = new Update();
        update.set("receiveSuccess",true);
        update.set("executeSuccess",true);

        // 更新Mongo
        mongoTemplate.updateFirst(query,update, MgMessageQueueLog.class);
    }


    /**
     * 更新Mongo，将此消息更新为已接收成功，但执行失败
     * @param message 消息
     * @return Boolean 布尔值
     */
    public static void receive(Message message,Exception e){
        MongoTemplate mongoTemplate = AppContextUtils.getBean(MongoTemplate.class);
        /* 查询条件 */
        Query query = new Query();
        query.addCriteria(
                Criteria.where("_id").is(RabbitHelper.getMsgId(message))
        );
        /* 更新字段 */
        Update update = new Update();
        update.set("receiveSuccess",true);
        update.set("executeSuccess",false);
        update.set("executeErrorMsg",e.getLocalizedMessage());
        // 更新Mongo
        mongoTemplate.updateFirst(query,update, MgMessageQueueLog.class);
    }

    public static <T> T getBodyEntity(Message message,Class<T> clazz){
        byte[] bytes = message.getBody();

        if (bytes.length == 0){
            throw new NullPointerException();
        }

        String str = new String(bytes,StandardCharsets.UTF_8);

        if (StringUtils.hasLength(str))
            return JSONObject.parseObject(str,clazz);
        else
            throw new NullPointerException();
    }


}
